package com.controller;

import com.model.OrderModel;
import com.services.IOrderServices;
import com.services.impl.OrderServicesImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @Program: exam
 * @ClassName: PutOrderServletController
 * @Description: 创建订单
 * @Author: li
 * @Create: 2024-12-23 16:02
 */
@WebServlet("/PutOrderServlet")
public class PutOrderServletController extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("application/json;charset=utf-8");
        
        try {
            String cid = request.getParameter("cid");
            String crent = request.getParameter("crent");
            String takeTime = request.getParameter("takeTime");
            String getTime = request.getParameter("getTime");
            String email = request.getParameter("email");
            String cname = request.getParameter("cname");

            OrderModel orderModel = new OrderModel(Integer.parseInt(cid),cname,email,Double.parseDouble(crent),takeTime,getTime,1);

            IOrderServices services = new OrderServicesImpl();
            services.addOrder(orderModel);

            // Return success code
            response.getWriter().print("{\"code\":0}");
        } catch (Exception e) {
            // Return error code
            response.getWriter().print("{\"code\":1,\"msg\":\"创建订单失败\"}");
        }
    }
}
